package com.raphon.weibo.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.raphon.weibo.R;
import com.weibo.net.AccessToken;
import com.weibo.net.DialogError;
import com.weibo.net.Utility;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;

public class LoginActivity extends Activity implements IWeiboActivity {

	private Button btn_regist;
	private Button btn_login;

	private EditText et_id;
	private EditText et_password;

	private static final String CONSUMER_KEY = "1007366622";
	private static final String CONSUMER_SECRET = "cc1c1e93505bee98a070008f15737d48";

	private Weibo weibo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.login);

		init();

		btn_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/*
				 * String id = et_id.getText().toString(); String password =
				 * et_password.getText().toString(); Intent intent = new
				 * Intent(); intent.putExtra("id", id);
				 * intent.putExtra("password", password);
				 * intent.setClass(LoginActivity.this, WebViewActivity.class);
				 * startActivity(intent);
				 */
				weibo = Weibo.getInstance();
				weibo.setupConsumerConfig(CONSUMER_KEY, CONSUMER_SECRET);
				weibo.setRedirectUrl("http://weibo.com");
				weibo.authorize(LoginActivity.this, new AuthDialogListener());

			}
		});
		btn_regist.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
	}

	class AuthDialogListener implements WeiboDialogListener {

		@Override
		public void onComplete(Bundle values) {
			// TODO Auto-generated method stub
			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");
			AccessToken accessToken = new AccessToken(token, CONSUMER_SECRET);
			accessToken.setExpiresTime(expires_in);
			Weibo.getInstance().setAccessToken(accessToken);
			Intent intent = new Intent();
			intent.setClass(LoginActivity.this, MainActivity.class);
			startActivity(intent);
		}

		@Override
		public void onWeiboException(WeiboException e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onError(DialogError e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub

		}

	}

	@Override
	public void init() {
		btn_login = (Button) this.findViewById(R.id.btn_login);
		btn_regist = (Button) this.findViewById(R.id.btn_regist);

		et_id = (EditText) this.findViewById(R.id.et_id);
		et_password = (EditText) this.findViewById(R.id.et_password);
	}

	@Override
	public void refresh(Object... params) {
		// TODO Auto-generated method stub

	}

	public void onResume() {
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		Utility.clearCookies(this);
		super.onDestroy();
	}
}
